package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;

public class PersonalInformation extends Screen {
	public enum GreetState {
		Greet0, Name, Greet1, Age, Greet2
	}

	GreetState state;
	Keyboard2 board;
	Message message;
	Box enter;
	Box enter2;
	Graphics graphics;
	double totalTime;
	float flickerSpeed;
	boolean flicker;
	float greet2Time;
	float greet2TimeLimit = 2;
	String userName;
	int age;

	public PersonalInformation(Game game) {
		super(game);
		graphics = game.getGraphics();
		board = new Keyboard2(graphics);
		message = new Message(graphics, "please enter your name...");
		enter = new Box(graphics, "FINISH", 2, 20, 3, 100, 435, 120, 38, Color.GRAY, Color.DKGRAY,
				Color.WHITE);
		enter2 = new Box(graphics, "FINISH", 2, 20, 3, 100, 300, 120, 38, Color.GRAY, Color.DKGRAY,
				Color.WHITE);
		state = GreetState.Greet0;
	}

	@Override
	public void update(float deltaTime) {
		totalTime += deltaTime;
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

		if (state == GreetState.Greet0)
			updateGreet0();
		if (state == GreetState.Name)
			updateName(touchEvents);
		if (state == GreetState.Greet1)
			updateGreet1();
		if (state == GreetState.Age)
			updateAge(touchEvents);
		if (state == GreetState.Greet2)
			updateGreet2(deltaTime);

	}

	private void updateGreet0() {
		if (message.finishedTyping) {
			state = GreetState.Name;
		}
	}

	private void updateName(List<TouchEvent> touchEvents) {
		if (totalTime > flickerSpeed) {
			flickerSpeed += .29;
			flicker = !flicker;
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP
				if (enter.click(event.x, event.y)) {
					userName = board.output;
					if (userName.length() < 3) {
						message = new Message(graphics, "Choose a longer name");
						state = GreetState.Greet0;
					} else {
						Settings.userName = userName;
						message = new Message(graphics, "How old are you?");
						board.setMode(4, false, true, true);
						state = GreetState.Greet1;
					}
				} else {
					board.click(event.x, event.y);
				}
			}
		}
	}

	private void updateGreet1() {
		if (message.finishedTyping) {
			state = GreetState.Age;
		}
	}

	private void updateAge(List<TouchEvent> touchEvents) {
		if (totalTime > flickerSpeed) {
			flickerSpeed += .29;
			flicker = !flicker;
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP
				if (enter2.click(event.x, event.y)) {
					age = Integer.parseInt(board.output);
					if (age < 3 || age > 98) {
						message = new Message(graphics, "I don't believe you.  please enter your real age");
						state = GreetState.Greet1;
					} else {
						message = new Message(graphics, userName + ", try your best");
						Settings.age = Integer.parseInt(board.output);
						state = GreetState.Greet2;
					}

				} else {
					board.click(event.x, event.y);
				}
			}
		}
	}

	private void updateGreet2(float deltaTime) {
		if (message.finishedTyping)
			greet2Time += deltaTime;
		if (greet2Time > greet2TimeLimit) {
			game.setScreen(new MainMenu2(game));

		}

	}

	@Override
	public void present(float deltaTime) {

		graphics.drawBackground(Color.BLACK); // BACKGROUND
		message.display(deltaTime); // Always display message

		if (state == GreetState.Greet0)
			presentGreet0();
		if (state == GreetState.Name)
			presentName();
		if (state == GreetState.Greet1)
			presentGreet1();
		if (state == GreetState.Age)
			presentAge();
		if (state == GreetState.Greet2)
			presentGreet2();
	}

	private void presentGreet0() {
	}

	private void presentName() {

		if (flicker)
			graphics.drawUniversalText(">", 2, false, 5, 120, 20, 255, Color.GREEN);
		graphics.drawUniversalText("" + board.output, 2, false, 17, 120, 20, 255, Color.GREEN);

		board.display();
		enter.draw();
	}

	private void presentGreet1() {
	}

	private void presentAge() {
		if (flicker)
			graphics.drawUniversalText(">", 2, false, 5, 120, 20, 255, Color.GREEN);
		graphics.drawUniversalText("" + board.output, 2, false, 17, 120, 20, 255, Color.GREEN);

		board.display();
		enter2.draw();
	}

	private void presentGreet2() {
	}

	@Override
	public void pause() {
		Settings.save(game.getFileIO());
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

}
